package jfox.platform.datadict.action;

import java.util.List;
import javax.ejb.EJB;

import jfox.platform.common.JSONResponse;
import jfox.platform.datadict.bo.DictCategoryBO;
import jfox.platform.datadict.bo.DictItemBO;
import jfox.platform.datadict.entity.DictCategory;
import jfox.platform.datadict.entity.DictItem;
import jfox.platform.infrastructure.SuperAction;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;


@Service(id = "dictmgmt")
public class DictMgmtAction extends SuperAction {
    @EJB
    DictCategoryBO dictCategoryBO;

    @EJB
    DictItemBO dictItemBO;

    @ActionMethod(name = "listcategorys", successView = "platform/dictmgmt/datadict_management.vhtml")
    public void listDictCategory(ActionContext actionContext) throws Exception {
        List<DictCategory> dictCategorys = dictCategoryBO.getAllCategory();

        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("dictCategorys", dictCategorys);
    }

    @ActionMethod(name = "listitems", successView = "include/json_response.vhtml", invocationClass = CategoryInvocation.class)
    public synchronized void getItemsByCategoryId(ActionContext actionContext) throws Exception {
        CategoryInvocation invocation = (CategoryInvocation)actionContext.getInvocation();
        List<DictItem> dictItems = dictItemBO.getDictItemsByCategoryCode(invocation.getCode());

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, dictItems);
    }


    @ActionMethod(name = "newcategory", successView = "platform/dictmgmt/dictcategory_new.vhtml")
    public void newDictCategory(ActionContext actionContext) throws Exception {
//        System.out.println("!!! Hello, " + invocationContext.getFullActionMethodName());
    }

    @ActionMethod(name = "newitem", successView = "platform/dictmgmt/dictitem_new.vhtml", invocationClass = CategoryInvocation.class)
    public void newDictItem(ActionContext actionContext) throws Exception {
        CategoryInvocation invocation = (CategoryInvocation)actionContext.getInvocation();
        //可以直接返回数值到页面
        //invocationContext.getPageContext().setAttribute("categoryId", invocation.getId());
        DictCategory dictCategory = dictCategoryBO.getCategoryById(invocation.getId());
        actionContext.getPageContext().setAttribute("dictcategory", dictCategory);

    }

    @ActionMethod(name = "editcategory", successView = "platform/dictmgmt/dictcategory_edit.vhtml", invocationClass = CategoryInvocation.class)
    public void editDictCategory(ActionContext actionContext) throws Exception {
        CategoryInvocation invocation = (CategoryInvocation)actionContext.getInvocation();
        DictCategory dictCategory = dictCategoryBO.getCategoryById(invocation.getId());
        actionContext.getPageContext().setAttribute("dictcategory", dictCategory);
    }

    @ActionMethod(name = "edititem", successView = "platform/dictmgmt/dictitem_edit.vhtml", invocationClass = ItemInvocation.class)
    public void editDictItem(ActionContext actionContext) throws Exception {
        ItemInvocation invocation = (ItemInvocation)actionContext.getInvocation();
        DictCategory dictCategory = dictCategoryBO.getCategoryByCode(invocation.getCategoryCode());
        actionContext.getPageContext().setAttribute("dictcategory", dictCategory);
        DictItem dictItem = dictItemBO.getDictItemById(invocation.getId());
        actionContext.getPageContext().setAttribute("dictItem", dictItem);
    }

    @ActionMethod(name = "createcategory", successView = "include/json_response.vhtml", invocationClass = CategoryInvocation.class)
    public synchronized void createDictCategory(ActionContext actionContext) throws Exception {
        CategoryInvocation invocation = (CategoryInvocation)actionContext.getInvocation();
        DictCategory dictcategory = new DictCategory();

        dictcategory.setCreator(getUserContext().getUsername());
        dictcategory.setPriority(99);
        dictcategory.setName(invocation.getName());
        dictcategory.setCode(invocation.getCode());
        dictcategory.setVstatus(0);
        dictcategory.setDescription(invocation.getDescription());
        dictcategory.setVersion(0);
        dictCategoryBO.newCategory(dictcategory);

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, dictcategory);
    }

    @ActionMethod(name = "createitem", successView = "include/json_response.vhtml", invocationClass = ItemInvocation.class)
    public synchronized void createDictItem(ActionContext actionContext) throws Exception {
        ItemInvocation invocation = (ItemInvocation)actionContext.getInvocation();
        DictItem dictItem = new DictItem();

        dictItem.setCreator(getUserContext().getUsername());
        dictItem.setPriority(invocation.getPriority());
        dictItem.setName(invocation.getName());
        dictItem.setStringValue(invocation.getStringValue());
        dictItem.setNumberValue(invocation.getValue());
        dictItem.setVstatus(0);
        dictItem.setDescription(invocation.getDescription());
        dictItem.setCategoryCode(invocation.getCategoryCode());
        dictItem.setVersion(0);
        //ITEM 表里有了CATEGORY的信息，即ITEM 引用了 CATEGORY，
        //所以要在DICTCATEGORY表里的REFS字段加入引用了CATEGORY的对象的说明，方便编辑和删除的判断；
        dictItemBO.newDictItem(dictItem);

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, dictItem);
    }

    @ActionMethod(name = "updatecategory", successView = "include/json_response.vhtml", invocationClass = CategoryInvocation.class)
    public synchronized void updateDictCategory(ActionContext actionContext) throws Exception {
        CategoryInvocation invocation = (CategoryInvocation)actionContext.getInvocation();
        DictCategory dictCategory = dictCategoryBO.getCategoryById(invocation.getId());
        dictCategory.setName(invocation.getName());
        dictCategory.setCode(invocation.getCode());
        dictCategory.setDescription(invocation.getDescription());
        dictCategory.setLastModifier(getUserContext().getUsername());
        dictCategory.setVersion(invocation.getVersion() + 1);
        dictCategoryBO.updateCategory(dictCategory);

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, dictCategory);
    }

    @ActionMethod(name = "updateitem", successView = "include/json_response.vhtml", invocationClass = ItemInvocation.class)
    public synchronized void updateDictItem(ActionContext actionContext) throws Exception {
        ItemInvocation invocation = (ItemInvocation)actionContext.getInvocation();
        DictItem dictItem = dictItemBO.getDictItemById(invocation.getId());
        dictItem.setName(invocation.getName());
        dictItem.setStringValue(invocation.getStringValue());
        dictItem.setNumberValue(invocation.getValue());
        dictItem.setPriority(invocation.getPriority());
        dictItem.setDescription(invocation.getDescription());
        dictItem.setLastModifier(getUserContext().getUsername());
        dictItem.setVersion(invocation.getVersion() + 1);
        dictItemBO.updateDictItem(dictItem);

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, dictItem);
    }


    @ActionMethod(name = "deletecategory", successView = "include/json_response.vhtml", invocationClass = CategoryInvocation.class)
    public synchronized void deleteDictCategory(ActionContext actionContext) throws Exception {
        CategoryInvocation invocation = (CategoryInvocation)actionContext.getInvocation();
        dictCategoryBO.deleteCategory(invocation.getId());
        JSONResponse jsonResponse = JSONResponse.createSuccessJSONResponse(null);
        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, null);
    }

    @ActionMethod(name = "deleteitem", successView = "include/json_response.vhtml", invocationClass = ItemInvocation.class)
    public synchronized void deleteDictItem(ActionContext actionContext) throws Exception {
        ItemInvocation invocation = (ItemInvocation)actionContext.getInvocation();
        dictItemBO.deleteDictItem(invocation.getId());
        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, null);
    }

    public static class CategoryInvocation extends Invocation {
        private long id;
        private String code;
        private String name;
        private String description;
        private int version;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public int getVersion() {
            return version;
        }

        public void setVersion(int version) {
            this.version = version;
        }

    }

    public static class ItemInvocation extends Invocation {
        private long id;
        private String categoryCode;
        private String stringValue;
        private int value;
        private int priority;
        private String name;
        private String description;
        private int version;

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public int getVersion() {
            return version;
        }

        public void setVersion(int version) {
            this.version = version;
        }

        public String getStringValue() {
            return stringValue;
        }

        public void setStringValue(String stringValue) {
            this.stringValue = stringValue;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getCategoryCode() {
            return categoryCode;
        }

        public void setCategoryCode(String categoryCode) {
            this.categoryCode = categoryCode;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public int getPriority() {
            return priority;
        }

        public void setPriority(int priority) {
            this.priority = priority;
        }

    }
}
